<template>
  <PageWrapDrawer
:show="show" :isUpdate="props.isUpdate" title-suffix="门店"
  @success="onSubmit"
  @on-drawer-close="onClose">
    <Form
name="basic"
          :model="insideData.data"
          :label-col="{ span: 5 }"
          :wrapper-col="{ span: 16 }"
          autocomplete="off"
          layout="horizontal"
    >
      <Row>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
          <FormItem label="门店名称" name="name" v-bind="validateInfos['data.name']">
            <a-input v-model:value="insideData.data.name"/>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
          <FormItem label="门店编号" name="code" v-bind="validateInfos['data.code']">
            <a-input v-model:value="insideData.data.code"/>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
          <FormItem label="联系人">
            <a-input v-model:value="insideData.data.contacts"/>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
          <FormItem label="电话">
            <a-input v-model:value="insideData.data.phone"/>
          </FormItem>
        </Col>
        <Col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" :xxl="24">
          <FormItem label="启用" name="enable">
            <RadioGroup v-model:value="insideData.data.enable">
              <Radio :value="1">启用</Radio>
              <Radio :value="2">禁用</Radio>
            </RadioGroup>
          </FormItem>
        </Col>
        <Col span="24">
          <FormItem label="详细地址" name="address" >
            <a-textarea 
            v-model:value="insideData.data.address" 
            :auto-size="{ minRows: 3, maxRows: 3, }"
            placeholder="请输入门店地址"/>
          </FormItem>
        </Col>
        <Col span="24">
          <FormItem label="备注" name="memo" >
            <a-textarea 
            v-model:value="insideData.data.memo" 
            :auto-size="{ minRows: 3, maxRows: 3, }"
            placeholder="备注一下..."/>
          </FormItem>
        </Col>
      </Row>
    </Form>
  </PageWrapDrawer>
</template>
<script lang="ts" name="shopDrawer" setup>
import {
Col,
Form, FormItem,
Radio, RadioGroup,
Row,
} from 'ant-design-vue';
import { reactive, toRaw, watchPostEffect } from 'vue';
import { create, update } from '/@/api/admin/shop';
import { PageWrapDrawer } from '/@/components/PageWrapDrawer';

const emit = defineEmits(['on-success', 'on-drawer-close'])
const props = defineProps({
    show: Boolean,
    isUpdate: Boolean,
    row: {type: [Object, null] as PropType<Object | null>, default: null,},
})

const useForm = Form.useForm;
const insideData = reactive({
  data: {
    id: null,
    code: '',
    name: '',
    contacts: '',
    phone: '',
    address: '',
    enable: 1,
    memo: '',
  }
});

const rulesRef = reactive({
  'data.name': [
    {required: true, message: '门店名称不能为空',},
  ] as unknown,
  'data.code': [
    {required: true, message: '门店编号不能为空',},
  ] as unknown,
});
watchPostEffect(async () => {
  let v1 = props.row as any
  if (v1) {
    insideData.data = {...v1}
  }
})

const {resetFields, validate, validateInfos} = useForm(insideData, rulesRef, {
  onValidate: (...args) => {
  },
});

const onSubmit = () => {
  validate()
    .then(async () => {
      let data = toRaw(insideData.data)
      if (props.isUpdate) {
        await update(data).then(() => {
          onClose()
        })
      } else {
        await create(data).then(() => {
          onClose()
        })
      }
      emit('on-success')
    }).catch(err => {
    console.log('error-----', err);
  });
}

const onClose = () => {
  Object.assign(insideData, {data: {}});
  resetFields();
  emit('on-drawer-close');
};

</script>
<style scoped>
</style>
